package com.qf.base.controller;

import com.alibaba.excel.EasyExcel;
import com.qf.base.domain.BaseCustomer;
import com.qf.base.listener.BaseCustomerDataListener;
import com.qf.base.service.IBaseCustomerService;
import com.qf.common.annotation.Log;
import com.qf.common.core.controller.BaseController;
import com.qf.common.core.domain.AjaxResult;
import com.qf.common.core.page.TableDataInfo;
import com.qf.common.enums.BusinessType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * --- ONE PIECE ---
 *
 * @author yangjiangcheng
 * @date 2025/9/1
 * @desc
 */
@RestController
@RequestMapping("/tms/base/customer")
public class BaseCustomerController extends BaseController {

    @Autowired
    private IBaseCustomerService baseCustomerService;

    /**
     * 获取客户信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BaseCustomer customer) {
        startPage();
        List<BaseCustomer> list = baseCustomerService.selectCustomerList(customer);
        System.out.println("列表数据");
        System.out.println(list);
        return getDataTable(list);
    }

    /**
     * 新增客户信息
     */
    @Log(title = "客户管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody BaseCustomer customer) {
        customer.setCreateBy(getUsername());
        return toAjax(baseCustomerService.insertCustomer(customer));
    }

    /**
     * 根据客户Id获取详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(baseCustomerService.selectCustomerById(id));
    }

    /**
     * 修改客户信息
     */
    @Log(title = "客户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody BaseCustomer customer) {
        customer.setUpdateBy(getUsername());
        return toAjax(baseCustomerService.updateCustomer(customer));
    }

    /**
     * 删除客户信息
     */
    @Log(title = "客户管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(baseCustomerService.deleteCustomerByIds(ids));
    }

    /**
     * 导入客户信息
     */
    @PostMapping("/import")
    @Log(title = "客户管理", businessType = BusinessType.IMPORT)
    public AjaxResult importCustomer(@RequestParam("file") MultipartFile file) {
        try {
            // 重点,此处第三个参数需要传入Service对象
            EasyExcel.read(file.getInputStream( ),
                            BaseCustomer.class,
                            new BaseCustomerDataListener(baseCustomerService))
                    .sheet( ).doRead( );
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }




}
